package ar.com.ford.it.fullEconomics.view.form;

import java.util.Calendar;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.util.MessageResources;
import org.apache.struts.validator.ValidatorForm;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese04Paypolicy;
import ar.com.ford.it.fullEconomics.persistence.dto.Teese05Paypolvers;
import ar.com.ford.it.fullEconomics.struct.FullEconomicWrapStruct;
import ar.com.ford.it.fullEconomics.util.FullEconomicsDateUtil;

public class PaymentPolicyDefinitionForm extends ValidatorForm {

    private String operation;
    private String section;
    private String supplierClass;
    private String supplierCode;
    private String itemCategory;
    private String item;
    private String itemScope;
    private String aprovedOnesOnly;
    private String dateIn;
    private String dateOut;
    private String value;
    private String prevDateIn;
    private String prevDateOut;
    private String prevValue;
    private String observations;
    private String createdDate;
    private String lastModifiedDate;
    private String proposedDate;
    private String approvedDate;
    private String appliesToSup;
    private String appliesToComm;
    private String appliesToPart;
    private Teese05Paypolvers prevPayPolVers;
    private Teese05Paypolvers payPolVersSelected;
    private FullEconomicWrapStruct fullEconomic;

    public void resetForm() {
        this.section = null;
        this.supplierClass = "-1";
        this.supplierCode = null;
        this.itemCategory = null;
        this.item = null;
        this.dateIn = null;
        this.dateOut = null;
        this.observations = null;
        this.itemScope = null;
        this.value = null;
        this.prevPayPolVers = null;
        this.payPolVersSelected = null;
        this.aprovedOnesOnly = "false";
        this.appliesToSup = "false";
        this.appliesToComm = "false";
        this.appliesToPart = "false";
        this.fullEconomic = null;
    }

    public ActionErrors validate(ActionMapping mapping,
                                 HttpServletRequest request) {

        ActionErrors ae = new ActionErrors();
        MessageResources resources = (MessageResources) request
                .getAttribute(org.apache.struts.Globals.MESSAGES_KEY);
        Calendar cal1 = null;
        Calendar cal2 = null;

        if (this.getSection().equalsIgnoreCase("save")) {
            if (this.getOperation().equals("newPayPol")) {
                if (this.getAppliesToSup().equals("true")) {
                    if (this.getSupplierClass().equals("-1")
                            || StringUtils.isBlank(this.getSupplierCode())) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.required",
                                                resources
                                                        .getMessage("paymentPolicyDefinition.label.supplier")));
                    }
                }
                if (this.getItemScope() == null) {
                    ae
                            .add(
                                    ActionMessages.GLOBAL_MESSAGE,
                                    new ActionMessage(
                                            "errors.required",
                                            resources
                                                    .getMessage("paymentPolicyDefinition.itemScope")));
                } else {
                    if (this.getItemCategory().equals("-1")) {
                        ae
                                .add(
                                        ActionMessages.GLOBAL_MESSAGE,
                                        new ActionMessage(
                                                "errors.required",
                                                resources
                                                        .getMessage("costStructure.itemCategory")));
                    }
                    if (this.getItemScope().equalsIgnoreCase("I")) {
                        if (this.getItem().equals("-1")) {
                            ae
                                    .add(
                                            ActionMessages.GLOBAL_MESSAGE,
                                            new ActionMessage(
                                                    "errors.required",
                                                    resources
                                                            .getMessage("costStructure.item")));
                        }
                    }
                }
            }
        }
        cal1 = FullEconomicsDateUtil.format(this.getDateIn());
        if (cal1 == null) {
            ae
                    .add(
                            ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage(
                                    "errors.invalid",
                                    resources
                                            .getMessage("paymentPolicyDefinition.label.dateIn")));
        }
        cal2 = FullEconomicsDateUtil.format(this.getDateOut());
        if (cal1 != null && cal2 != null) {
            if (FullEconomicsDateUtil.getDifference(this.getDateIn(), this
                    .getDateOut()) <= 0) {
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.date.range",
                                        resources
                                                .getMessage("currencyValues.label.tableHeader.fechaOut"),
                                        resources
                                                .getMessage("currencyValues.label.tableHeader.fechaIn")));
            }
        }
        try {
            if (Double.valueOf(this.getValue()) < 0
                    || Double.valueOf(this.getValue()) > 100)
                ae
                        .add(
                                ActionMessages.GLOBAL_MESSAGE,
                                new ActionMessage(
                                        "errors.invalid",
                                        resources
                                                .getMessage("paymentPolicyDefinition.label.value")));
        } catch (Exception e) {
            ae
                    .add(
                            ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage(
                                    "errors.invalid",
                                    resources
                                            .getMessage("paymentPolicyDefinition.label.value")));
        }
        if (this.getObservations().length() > 500) {
            ae
                    .add(
                            ActionMessages.GLOBAL_MESSAGE,
                            new ActionMessage(
                                    "customMessage",
                                    resources
                                            .getMessage("paymentPolicyDefinition.error.observations.lenght")));
        }
        return ae;
    }

    public void setPaymentPolicyDetails() {
        Teese04Paypolicy paymentPolicy;
        if (this.getPayPolVersSelected() != null)
            paymentPolicy = this.getPayPolVersSelected().getTeese04Paypolicy();
        else if (this.getPrevPayPolVers() != null)
            paymentPolicy = this.getPrevPayPolVers().getTeese04Paypolicy();
        else
            paymentPolicy = new Teese04Paypolicy();
        if (paymentPolicy.getTeess01Supplier() != null) {
            this.setSupplierClass(paymentPolicy.getTeess01Supplier()
                    .getEess01SupplierClassCodeC());
            this.setSupplierCode(paymentPolicy.getTeess01Supplier()
                    .getEess01SupplierCodeC());
        } else {
            this.setSupplierClass("-1");
        }
        this.setItemCategory(paymentPolicy.getTeesc05Itemcatgry()
                .getEesc05ItemcatgryNumberK().toString());
        if (paymentPolicy.getEese04PaypolItemTypeCodeC().equalsIgnoreCase("I")) {
            this.setItem(paymentPolicy.getTeesc06Item().getEesc06ItemNumberK()
                    .toString());
            this.setItemScope("I");
        } else {
            this.setItemScope("C");
        }
    }

    public void setVersionDetails() {
        Teese05Paypolvers payPolVers = this.getPayPolVersSelected();
        Calendar calDateIn = Calendar.getInstance();
        Calendar calDateOut = Calendar.getInstance();
        Calendar calDateCreate = Calendar.getInstance();
        Calendar calDateModif = Calendar.getInstance();
        Calendar calDatePropose = Calendar.getInstance();
        Calendar calDateApprove = Calendar.getInstance();
        calDateIn.setTime(payPolVers.getEese05PpyverInDateY());
        calDateOut.setTime(payPolVers.getEese05PpyverOutDateY());
        calDateCreate.setTime(payPolVers.getEese05CreateS());
        calDateModif.setTime(payPolVers.getEese05LastUpdtS());
        if (payPolVers.getEese05PpyverProposalS() != null)
            calDatePropose.setTime(payPolVers.getEese05PpyverProposalS());
        if (payPolVers.getEese05PpyverApprovalS() != null)
            calDateApprove.setTime(payPolVers.getEese05PpyverApprovalS());
        this.setDateIn(FullEconomicsDateUtil.format(calDateIn));
        this.setDateOut(FullEconomicsDateUtil.format(calDateOut));
        this.setValue(payPolVers.getEese05PpyverPercentP().toString());
        this.setObservations(payPolVers.getEese05PpyverCommentsX());
        this.setCreatedDate(FullEconomicsDateUtil.format(calDateCreate));
        this.setLastModifiedDate(FullEconomicsDateUtil.format(calDateModif));
        this.setProposedDate(FullEconomicsDateUtil.format(calDatePropose));
        this.setApprovedDate(FullEconomicsDateUtil.format(calDateApprove));
    }

    public void setPrevVersionDetails() {
        Teese05Paypolvers payPolVers = this.getPrevPayPolVers();
        Calendar calDateIn = Calendar.getInstance();
        Calendar calDateOut = Calendar.getInstance();
        Calendar calDateCreate = Calendar.getInstance();
        Calendar calDateModif = Calendar.getInstance();
        calDateIn.setTime(payPolVers.getEese05PpyverInDateY());
        calDateOut.setTime(payPolVers.getEese05PpyverOutDateY());
        calDateCreate.setTime(payPolVers.getEese05CreateS());
        calDateModif.setTime(payPolVers.getEese05LastUpdtS());
        this.setPrevDateIn(FullEconomicsDateUtil.format(calDateIn));
        this.setPrevDateOut(FullEconomicsDateUtil.format(calDateOut));
        this.setPrevValue(payPolVers.getEese05PpyverPercentP().toString());
    }

    public String getItem() {
        return item;
    }

    public void setItem(String item) {
        this.item = item;
    }

    public String getItemCategory() {
        return itemCategory;
    }

    public void setItemCategory(String itemCategory) {
        this.itemCategory = itemCategory;
    }

    public String getSection() {
        return section;
    }

    public void setSection(String section) {
        this.section = section;
    }

    public String getSupplierClass() {
        return supplierClass;
    }

    public void setSupplierClass(String supplierClass) {
        this.supplierClass = supplierClass;
    }

    public String getSupplierCode() {
        return supplierCode;
    }

    public void setSupplierCode(String supplierCode) {
        this.supplierCode = supplierCode.toUpperCase();
    }

    public String getAprovedOnesOnly() {
        return aprovedOnesOnly;
    }

    public void setAprovedOnesOnly(String aprovedOnesOnly) {
        this.aprovedOnesOnly = aprovedOnesOnly;
    }

    public Teese05Paypolvers getPrevPayPolVers() {
        return prevPayPolVers;
    }

    public void setPrevPayPolVers(Teese05Paypolvers payPolVers) {
        this.prevPayPolVers = payPolVers;
    }

    public String getDateIn() {
        return dateIn;
    }

    public void setDateIn(String dateIn) {
        this.dateIn = dateIn;
    }

    public String getDateOut() {
        return dateOut;
    }

    public void setDateOut(String dateOut) {
        this.dateOut = dateOut;
    }

    public Teese05Paypolvers getPayPolVersSelected() {
        return payPolVersSelected;
    }

    public void setPayPolVersSelected(Teese05Paypolvers payPolVersSelected) {
        this.payPolVersSelected = payPolVersSelected;
    }

    public String getObservations() {
        return observations;
    }

    public void setObservations(String observations) {
        this.observations = observations;
    }

    public String getApprovedDate() {
        return approvedDate;
    }

    public void setApprovedDate(String approvedDate) {
        this.approvedDate = approvedDate;
    }

    public String getCreatedDate() {
        return createdDate;
    }

    public void setCreatedDate(String createdDate) {
        this.createdDate = createdDate;
    }

    public String getProposedDate() {
        return proposedDate;
    }

    public void setProposedDate(String proposedDate) {
        this.proposedDate = proposedDate;
    }

    public String getOperation() {
        return operation;
    }

    public void setOperation(String operation) {
        this.operation = operation;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

    public String getItemScope() {
        return itemScope;
    }

    public void setItemScope(String itemScope) {
        this.itemScope = itemScope;
    }

    public String getAppliesToComm() {
        return appliesToComm;
    }

    public void setAppliesToComm(String appliesToComm) {
        this.appliesToComm = appliesToComm;
    }

    public String getAppliesToPart() {
        return appliesToPart;
    }

    public void setAppliesToPart(String appliesToPart) {
        this.appliesToPart = appliesToPart;
    }

    public String getAppliesToSup() {
        return appliesToSup;
    }

    public void setAppliesToSup(String appliesToSup) {
        this.appliesToSup = appliesToSup;
    }

    public void reset(ActionMapping mapping, HttpServletRequest request) {
        this.appliesToSup = "false";
        this.appliesToComm = "false";
        this.appliesToPart = "false";
    }

    public String getLastModifiedDate() {
        return lastModifiedDate;
    }

    public void setLastModifiedDate(String lastModifiedDate) {
        this.lastModifiedDate = lastModifiedDate;
    }

    public String getPrevDateIn() {
        return prevDateIn;
    }

    public void setPrevDateIn(String prevDateIn) {
        this.prevDateIn = prevDateIn;
    }

    public String getPrevDateOut() {
        return prevDateOut;
    }

    public void setPrevDateOut(String prevDateOut) {
        this.prevDateOut = prevDateOut;
    }

    public String getPrevValue() {
        return prevValue;
    }

    public void setPrevValue(String prevValue) {
        this.prevValue = prevValue;
    }

    public FullEconomicWrapStruct getFullEconomic() {
        return fullEconomic;
    }

    public void setFullEconomic(FullEconomicWrapStruct fullEconomic) {
        this.fullEconomic = fullEconomic;
    }

}
